package com.wzx.ptplatform.service.impl;

import com.wzx.ptplatform.dao.StudentDao;
import com.wzx.ptplatform.domain.Student;
import com.wzx.ptplatform.domain.University;
import com.wzx.ptplatform.service.StudentService;
import com.wzx.ptplatform.utils.PtplatformException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @author 紫煦桉
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;

    @Override
    public Page<Student> findList(Student student, Integer page, Integer limit) {
        Specification<Student> spec = (Specification<Student>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            List<Expression<Boolean>> exList = predicate.getExpressions();//动态SQL表达式集合
            if (student.getStudentName() != null) {
                exList.add(criteriaBuilder.like(root.get("studentName").as(String.class), "%" + student.getStudentName() + "%"));
            }
            if (student.getUniversity() != null && student.getUniversity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("university").get("id").as(Long.class), student.getUniversity().getId()));
            }
            if (student.getMajor() != null && student.getMajor().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("major").get("id").as(Long.class), student.getMajor().getId()));
            }
            if (student.getCity() != null && student.getCity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("city").get("id").as(Long.class), student.getCity().getId()));
            }
            return predicate;
        };
        PageRequest pageable = PageRequest.of(page, limit, Sort.Direction.DESC, "id");
        return studentDao.findAll(spec, pageable);
    }

    @Override
    public List<Student> findAll() throws PtplatformException {
        return studentDao.findAll();
    }

    @Override
    public Page<Student> findPtList(Student student, Integer page, Integer limit) throws PtplatformException {
        Specification<Student> spec = (Specification<Student>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            List<Expression<Boolean>> exList = predicate.getExpressions();//动态SQL表达式集合
            if (student.getStudentName() != null) {
                exList.add(criteriaBuilder.like(root.get("studentName").as(String.class), "%" + student.getStudentName() + "%"));
            }
            if (student.getUniversity() != null && student.getUniversity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("university").get("id").as(Long.class), student.getUniversity().getId()));
            }
            if (student.getMajor() != null && student.getMajor().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("major").get("id").as(Long.class), student.getMajor().getId()));
            }
            if (student.getCity() != null && student.getCity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("city").get("id").as(Long.class), student.getCity().getId()));
            }
            exList.add(criteriaBuilder.isNotNull(root.get("company")));
            return predicate;
        };
        PageRequest pageable = PageRequest.of(page, limit, Sort.Direction.DESC, "id");
        return studentDao.findAll(spec, pageable);
    }

    @Override
    public Student login(Student student) throws PtplatformException {
        Student studentMessage = studentDao.findByEmail(student.getEmail());
        if (studentMessage != null && studentMessage.getPassword().equals(student.getPassword())) {
            return studentMessage;
        }
        return null;
    }

    @Override
    public boolean saveOrEdit(Student student) throws PtplatformException {
        try {
            studentDao.saveAndFlush(student);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Student findById(long id) throws PtplatformException {
        return studentDao.findById(id).orElse(null);
    }

    @Override
    public boolean deleteById(long id) throws PtplatformException {
        try {
            studentDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
